package org.keyintegrity.webbeans;

import java.util.HashMap;
import java.util.Map;

import org.apache.tapestry.components.IPrimaryKeyConverter;

public class ListConverter implements IPrimaryKeyConverter {

    private Map<String, Object> objectCache = new HashMap<String, Object>();
    private BeanEditorTypeMappings typeMappings;
    
    public ListConverter(BeanEditorTypeMappings typeMappings) {
        this.typeMappings = typeMappings;
    }
    
    public Object getPrimaryKey(Object value) {
        String key = String.valueOf(typeMappings.getBeanEditor().getBeanFieldName() 
                + "["
                + typeMappings.getListEntryIndex()
                + "]");
        
        objectCache.put(key, value);
        
        return value.toString();
    }

    public Object getValue(Object primaryKey) {
        return objectCache.get(primaryKey);
    }

}
